<template>
  <div id="app" style="font-size: 26px; font-weght: bold; margin: 30px 0">
    <div class="all">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="水票名称">
              <el-input
                v-model="formInline.name"
                placeholder="请输入水票名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="水票类型">
              <el-select
                v-model="formInline.user"
                clearable
                placeholder="选择水票类型"
                @change="selectChange"
              >
                <el-option
                  v-for="(item, index) in Data"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="上下架">
              <el-select
                v-model="formInline.region"
                clearable
                placeholder="是否上下架"
                @change="fluctuate"
              >
                <el-option label="上架" value="1"></el-option>
                <el-option label="下架" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="box">
              <el-button type="primary" @click="onSubmit">查询</el-button>
              <el-button type="primary" @click="onReset">重置</el-button>
              <el-button type="primary" @click="addTicket">新增水票</el-button>
              <!-- suffix -->
            </el-form-item>
          </el-form>
        </div>
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          style="width: 100%"
          max-height="600"
        >
          <el-table-column
            label="序号"
            align="center"
            type="index"
            :index="indexMethod"
            width="50"
          >
          </el-table-column>
          <el-table-column
            prop="water_name"
            align="center"
            label="水票名称"
            width=""
          >
          </el-table-column>
          <el-table-column
            prop="goods_cat_name"
            align="center"
            label="水票类型"
            width="120"
          >
          </el-table-column>
          <el-table-column
            prop="costprice"
            align="center"
            label="水票原价"
            width="80"
          >
          </el-table-column>
          <el-table-column
            prop="price"
            align="center"
            label="水票现价"
            width="80"
          >
          </el-table-column>
          <el-table-column
            prop="water_number"
            align="center"
            label="套餐库存"
            width="80"
          >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            align="center"
            prop="water_img"
            label="水票图片"
            width="120"
          >
            <template slot-scope="scope">
              <el-image
                style="width: 50px; height: 50px"
                :src="scope.row.water_img"
                fit="cover"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column
            prop="buy_count"
            align="center"
            label="出售量"
            width="80"
          >
          </el-table-column>
          <el-table-column prop="goods_name" align="center" label="商品名称">
          </el-table-column>
          <el-table-column
            prop="package_nember"
            align="center"
            label="套餐水数量(桶/瓶)"
            width="100"
          >
          </el-table-column>
          <el-table-column prop="ctime" align="center" label="创建时间">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            align="center"
            prop="value"
            label="状态"
            width="140"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.is_up"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="上架"
                inactive-text="下架"
                :active-value="1"
                :inactive-value="0"
                @change="switchCage(scope.index, scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                @click="handleEdit(scope.row)"
                >删除</el-button
              >

              <el-button size="mini" type="primary" @click="handle(scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[50, 100, 150, 200]"
            :page-size="100"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>

        <!-- 新增水票 -->
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          :before-close="handleC"
          width="35%"
        >
          <el-form
            :model="formLabelAlign"
            label-width="120px"
            ref="managementForm"
            :rules="rules"
          >
            <div class="oneLine">
              <el-form-item prop="water_name" label="水票名称">
                <el-input v-model="formLabelAlign.water_name"></el-input>
              </el-form-item>
            </div>
            <div class="oneLine">
              <el-form-item prop="water_number" label="水票库存数量">
                <el-input v-model="formLabelAlign.water_number"></el-input>
              </el-form-item>
            </div>
            <div class="oneLine">
              <el-form-item prop="costprice" label="水票原价">
                <el-input v-model="formLabelAlign.costprice"></el-input>
              </el-form-item>
            </div>
            <div class="oneLine">
              <el-form-item prop="price" label="水票现价">
                <el-input v-model="formLabelAlign.price"></el-input>
              </el-form-item>
            </div>
            <div class="oneLine">
              <el-form-item label="水票类型" prop="water_cat_id">
                <el-select
                  v-model="formLabelAlign.water_cat_id"
                  placeholder="选择水票类型"
                >
                  <el-option
                    v-for="(item, index) in Data"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
            <div class="oneLine">
              <el-form-item label="商品名称">
                <el-button-group>
                  <el-button @click="innerVisibleChange">
                    {{
                      selectGoodsDate.name
                        ? selectGoodsDate.name
                        : "请选择商品名称"
                    }}
                  </el-button>
                </el-button-group>
              </el-form-item>
            </div>
            <div class="oneLine">
              <el-form-item label="套餐水数量" prop="package_nember">
                <el-input v-model="formLabelAlign.package_nember"></el-input>
              </el-form-item>
              <span class="bucketUnit">桶</span>
            </div>
            <div class="oneLine">
              <el-form-item label="水票状态" prop="is_up">
                <el-switch
                  v-model="formLabelAlign.is_up"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="上架"
                  inactive-text="下架"
                  :active-value="1"
                  :inactive-value="0"
                  @change="switchChange(formLabelAlign.is_up)"
                >
                </el-switch>
              </el-form-item>
            </div>
            <div class="oneLine">
              <el-form-item label="是否推荐热销" prop="is_hot">
                <el-switch
                  v-model="formLabelAlign.is_hot"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="是"
                  inactive-text="否"
                  :active-value="1"
                  :inactive-value="2"
                  @change="switchChange(formLabelAlign.is_up)"
                >
                </el-switch>
              </el-form-item>
            </div>
            <div class="oneLine">
              <el-form-item label="水票图片" prop="imageUrl">
                <el-upload
                  class="upload-demo"
                  action
                  :http-request="uploadFiles"
                  ref="upload"
                  :limit="fileLimit"
                  :on-remove="handleRemove"
                  :file-list="fileList"
                  :on-exceed="handleExceed"
                  :before-upload="beforeUpload"
                  :show-file-list="false"
                  :headers="headers"
                  :on-change="fileChange"
                >
                  <el-image
                    v-if="formLabelAlign.imageUrl"
                    class="imageGood"
                    :src="formLabelAlign.imageUrl"
                    style="width: 360px; height: 180px"
                  ></el-image>
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  <div slot="tip" class="el-upload__tip">
                    只能上传jpg/png文件，且不超过5M
                  </div>
                </el-upload>
              </el-form-item>
            </div>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitData('managementForm')"
              >保 存</el-button
            >
          </span>
        </el-dialog>

        <el-dialog
          width="50%"
          :visible.sync="innerVisible"
          append-to-body
          :before-close="handleClose"
        >
          <el-form
            :inline="true"
            ref="information"
            :model="formGoodsSearch"
            class="demo-form-inline"
          >
            <el-form-item label="商品名称" class="distance">
              <el-input
                v-model="formGoodsSearch.name"
                placeholder="请输入商品"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品编号">
              <el-input
                v-model="formGoodsSearch.bn"
                placeholder="请输入商品编号"
              ></el-input>
            </el-form-item>
            <el-button type="primary" @click="onSubmitUser">查询</el-button>
            <div class="demonstrate">
              <el-input
                :readonly="true"
                v-model="exhibition"
                placeholder="展示商品选项的水票名称"
              ></el-input>
            </div>

            <div style="border: 1px solid #d0ccccce" class="demonstrate">
              <el-table
                class="innerTab"
                ref="singleTable"
                :data="tableDate"
                highlight-current-row
                border
                @current-change="handleSelectionChange"
                style="width: 100%"
                max-height="600"
              >
                <el-table-column prop="id" align="center" label="ID" width="60">
                </el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="images"
                  label="图片"
                  width="100"
                >
                  <template slot-scope="scope">
                    <el-image
                      style="width: 80px; height: 80px"
                      :src="scope.row.images"
                      fit="cover"
                    ></el-image>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="name"
                  align="center"
                  label="名称"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  prop="bn"
                  align="center"
                  label="商品编号"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  prop="brand_name"
                  align="center"
                  label="品牌"
                  show-overflow-tooltip
                >
                </el-table-column>
                <!-- <el-table-column prop="goods_type_id" align="center" label="商品类型" show-overflow-tooltip>
                </el-table-column> -->
                <el-table-column
                  prop="stock"
                  label="库存"
                  align="center"
                  show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                  prop="price"
                  label="销售价"
                  align="center"
                  show-overflow-tooltip
                >
                </el-table-column>
              </el-table>
            </div>
          </el-form>
          <el-pagination
            @size-change="handleSizeChange1"
            @current-change="handleCurrentChange1"
            :current-page="page1"
            :page-sizes="[50, 100, 150, 200]"
            :page-size="100"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total1"
            class="demonstrate"
          >
          </el-pagination>
          <span slot="footer">
            <el-button type="primary" @click="selectGoods" class="buttonUser"
              >确定</el-button
            >
          </span>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";
import {
  ticketing,
  ticketUser,
  uploadFiles,
  classification,
  goodsList,
  addition,
  modification,
  Package_del,
} from "../../../../utils/api.js";
import { compressImage } from "../../../../assets/js/compressImage";
export default {
  data() {
    return {
      dialogTitle: "",
      marketable: 1,
      goods_type_id: 1,
      loading: false,
      token: sessionStorage.getItem("token"),
      formInline: {
        name: "",
        user: "",
        region: "",
        waterCatId: "",
        isUp: "",
      },
      //添加水票
      formLabelAlign: {
        buy_count: "",
        costprice: "",
        ctime: "",
        goods_cat_name: "",
        goods_id: "",
        goods_name: "",
        goods_type: "",
        id: "",
        is_up: "",
        package_nember: "",
        price: "",
        unit: "",
        water_cat_id: "",
        imageUrl: "",
        water_name: "",
        water_number: "",
        //上传后端的图片
        imageUrl1: "",
      },
      //添加水票搜索商品
      formGoodsSearch: {
        name: "",
        bn: "",
      },
      imgHeader: this.Global.imgHeader,

      //分页器
      page: 1,
      limit: 10,
      total: 0,
      page1: 1,
      limit1: 6,
      total1: 0,
      //新增票务名称展示
      exhibition: "",
      //票务单选
      radio1: "1",
      radio2: "1",
      //商品选中
      exhibition: "",
      //新增图片名称弹框
      innerVisible: false,
      //新增票务弹框
      dialogVisible: false,
      //展示上下架
      value: true,
      // checkNum: (rule, value, callback) => {
      //   var reg = /^[0-9]+.?[0-9]*$/;
      //   if (!reg.test(value)) {
      //     callback(new Error("请输入数字"));
      //   }
      // },
      rules: {
        water_name: [
          { required: true, message: "请输入水票名称", trigger: "blur" },
        ],
        water_number: [
          {
            required: true,
            message: "请输入水票数量",
            trigger: "blur",
          },
          // {
          //   type: "number",
          //   message: "请输入数字",
          //   trigger: "blur",
          // },
          {
            validator: (rule, value, callback) => {
              var reg = /^[1-9]+[0-9]*]*$/;
              if (!reg.test(value)) {
                callback(new Error("请输入正整数"));
              }
              if (value > 99999999.99) {
                callback(new Error("不能大于99999999.99"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        costprice: [
          {
            required: true,
            message: "请输入水票原价",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              var reg = /^(?!0+(?:\.0+)?$)(?:0|[1-9]\d*)(?:\.\d{1,2})?$/;
              if (!reg.test(value)) {
                callback(new Error("请输入大于0且不超过两位小数的数字"));
              }
              if (value > 99999999.99) {
                callback(new Error("不能大于99999999.99"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
          // {
          //   type: "number",
          //   message: "请输入数字",
          //   trigger: "blur",
          // },
        ],
        price: [
          { required: true, message: "请输入水票现价", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              var reg = /^(?!0+(?:\.0+)?$)(?:0|[1-9]\d*)(?:\.\d{1,2})?$/;
              if (!reg.test(value)) {
                callback(new Error("请输入大于0且不超过两位小数的数字"));
              }
              if (value > 99999999.99) {
                callback(new Error("不能大于99999999.99"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
          // {
          //   type: "number",
          //   message: "请输入数字",
          //   trigger: "blur",
          // },
        ],
        water_cat_id: [
          { required: true, message: "请选择水票分类", trigger: "blur" },
        ],
        package_nember: [
          { required: true, message: "请输入套餐水数量", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              var reg = /^[1-9]+[0-9]*]*$/;
              if (!reg.test(value)) {
                callback(new Error("请输入正整数"));
              }
              if (value > 99999999.99) {
                callback(new Error("不能大于99999999.99"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
          // {
          //   type: "number",
          //   message: "请输入数字",
          //   trigger: "blur",
          // },
        ],
        original: [
          { required: true, message: "请选择水票状态", trigger: "blur" },
        ],
        imageUrl: [{ required: true, message: "请上传图片", trigger: "blur" }],
      },
      //上传
      fileList: [],
      fileType: ["png", "jpg", "jpeg"],
      fileLimit: 1,
      headers: { "Content-Type": "multipart/form-data" },
      srcList2: {},
      tableData: [
        {
          id: "",
          water_name: "",
          water_number: "",
          goods_id: "",
          package_nember: "",
          is_up: "",
          imageUrl: "",
          imageUrl1: "",
          water_cat_id: "",
          goods_type: "",
          buy_count: "",
          price: "",
          ctime: "",
          costprice: "",
          unit: "",
          goods_name: "",
          goods_cat_name: "",
        },
      ],

      Data: [
        {
          id: "",
          name: "",
        },
      ],
      tableDate: [
        {
          id: "",
          images: "",
          name: "",
          bn: "",
          brand_name: "",
          goods_type_id: "",
          stock: "",
          price: "",
        },
      ],
      selectGoodsDate: {
        id: "",
        images: "",
        name: "",
        bn: "",
        brand_name: "",
        goods_type_id: "",
        stock: "",
        price: "",
      },
    };
  },
  methods: {
    addTicket() {
      this.dialogTitle = "新增";
      this.dialogVisible = true;
      (this.formLabelAlign.buy_count = ""),
        (this.formLabelAlign.costprice = ""),
        (this.formLabelAlign.ctime = ""),
        (this.formLabelAlign.goods_cat_name = ""),
        (this.formLabelAlign.goods_id = ""),
        (this.formLabelAlign.goods_name = ""),
        (this.formLabelAlign.goods_type = ""),
        (this.formLabelAlign.id = ""),
        (this.formLabelAlign.is_up = ""),
        (this.formLabelAlign.package_nember = ""),
        (this.formLabelAlign.price = ""),
        (this.formLabelAlign.unit = ""),
        (this.formLabelAlign.water_cat_id = ""),
        (this.formLabelAlign.water_img = ""),
        (this.formLabelAlign.water_name = ""),
        (this.formLabelAlign.water_number = "");
      (this.exhibition = ""),
        (this.formLabelAlign.product = ""),
        (this.formLabelAlign.number = "");
      this.formLabelAlign.imageUrl = "";
      this.selectGoodsDate = {};
    },
    fluctuate(isUp) {
      this.formInline.isUp = isUp;
      this.invoking();
    },
    selectChange(waterCatId) {
      this.formInline.waterCatId = waterCatId;
      this.invoking();
    },
    onSubmit() {
      this.page = 1;
      this.invoking();
    },
    onReset() {
      (this.formInline.name = ""),
        (this.formInline.waterCatId = ""),
        (this.formInline.isUp = ""),
        (this.formInline.user = "");
      this.formInline.region = "";
      this.invoking();
    },
    // 序号排序
    indexMethod(index) {
      if (this.token < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.page - 1) * this.limit - index;
      }
    },

    clearData() {
      this.$refs.managementForm.resetFields();
      (this.formLabelAlign.buy_count = ""),
        (this.formLabelAlign.costprice = ""),
        (this.formLabelAlign.ctime = ""),
        (this.formLabelAlign.goods_cat_name = ""),
        (this.formLabelAlign.goods_id = ""),
        (this.formLabelAlign.goods_name = ""),
        (this.formLabelAlign.goods_type = ""),
        (this.formLabelAlign.id = ""),
        (this.formLabelAlign.is_up = ""),
        (this.formLabelAlign.package_nember = ""),
        (this.formLabelAlign.price = ""),
        (this.formLabelAlign.unit = ""),
        (this.formLabelAlign.water_cat_id = ""),
        (this.formLabelAlign.water_img = ""),
        (this.formLabelAlign.water_name = ""),
        (this.formLabelAlign.water_number = "");
      (this.exhibition = ""),
        (this.formLabelAlign.product = ""),
        (this.formLabelAlign.number = "");
      (this.formLabelAlign.water_img1 = ""), (this.selectGoodsDate = {});
    },

    //关闭弹框
    handleClose(done) {
      done();
    },
    //关闭弹窗
    handleC(done) {
      done();
      this.clearData();
    },
    selectGoods() {
      if (this.selectGoodsDate.id) {
        this.innerVisible = false;
      } else {
        this.$message({
          message: "请选择商品",
          type: "warning",
        });
      }
    },
    innerVisibleChange() {
      this.innerVisible = true;
      this.getInnerGoodsList();
    },
    //商品
    getInnerGoodsList() {
      goodsList(
        this.token,
        this.page1,
        this.limit1,
        this.goods_type_id,
        "",
        this.marketable,
        this.formGoodsSearch.name,
        this.formGoodsSearch.bn,
        "",
        // ""
        this.formLabelAlign.water_cat_id,
        "",
        ""
      ).then((res) => {
        if (res.data.status) {
          this.total1 = res.data.data.count;
          this.tableDate = res.data.data.data;
          this.tableDate.forEach((item) => {
            if (item.images) {
              item.images = this.imgHeader + item.images;
            }
            if (this.selectGoodsDate.id === item.id) {
              this.$refs.singleTable.setCurrentRow(item);
            }
          });
        }
      });
    },
    selectTicket(ticket) {
      this.exhibition = ticket;
    },
    submitData(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.selectGoodsDate.id == undefined) {
            this.$message.warning("请选择商品名称");
            return;
          }
          this.dialogVisible = false;
          console.log(this.formLabelAlign);
          console.log("this.selectGoodsDate.id", this.selectGoodsDate.id);
          if (this.formLabelAlign.id) {
            // 编辑
            modification(
              this.token,
              this.formLabelAlign.id,
              this.formLabelAlign.water_name,
              this.formLabelAlign.water_number,
              this.selectGoodsDate.id,
              this.formLabelAlign.package_nember,
              this.formLabelAlign.is_up,
              this.formLabelAlign.water_img1,
              this.formLabelAlign.water_cat_id,
              this.formLabelAlign.price,
              this.formLabelAlign.costprice,
              this.formLabelAlign.is_hot
            ).then((res) => {
              if (res.data.status) {
                this.$message({
                  message: "修改成功",
                  type: "success",

                  onClose: (res) => {},
                });
                this.invoking();
                this.outerVisible = false;
                this.clearData();
                this.$refs[formName].resetFields();
                //  this.brandList(this.token,this.currentPage,this.limit,this.topInputList[0].text)
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            // 新增
            // console.log(this.formLabelAlign);
            addition(
              this.token,
              this.formLabelAlign.water_name,
              this.formLabelAlign.water_number,
              this.selectGoodsDate.id,
              this.formLabelAlign.package_nember,
              this.formLabelAlign.is_up,
              this.formLabelAlign.water_img1,
              this.formLabelAlign.water_cat_id,
              this.formLabelAlign.price,
              this.formLabelAlign.costprice,
              this.formLabelAlign.is_hot
            ).then((res) => {
              if (res.data.status) {
                this.$message({
                  message: "保存成功",
                  type: "success",

                  onClose: (res) => {},
                });
                this.invoking();
                this.outerVisible = false;
                this.$refs[formName].resetFields();
                this.clearData();
                //  this.brandList(this.token,this.currentPage,this.limit,this.topInputList[0].text)
              } else {
                this.$message.error(res.data.msg);
              }
            });
          }
        }
      });
    },
    //分页器
    handleSizeChange(val) {
      this.limit = val;
      this.invoking();
    },
    handleSizeChange1(val) {
      this.limit1 = val;
      this.getInnerGoodsList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.invoking();
    },
    handleCurrentChange1(val) {
      this.page1 = val;
      this.getInnerGoodsList();
    },
    //上传
    uploadFile() {},
    handleRemove() {},
    //超出文件个数的回调
    handleExceed() {
      this.$message({
        type: "warning",
        message: "超出最大上传文件数量的限制！",
      });
      return;
    },
    //上传文件的格式
    beforeUpload(file) {
      if (file.type != "" || file.type != null || file.type != undefined) {
        //截取文件的后缀，判断文件类型
        const FileExt = file.name.replace(/.+\./, "").toLowerCase();
        //计算文件的大小
        const isLt5M = file.size / 1024 / 1024 < 1; //这里做文件大小限制
        //如果大于50M
        if (!isLt5M) {
          this.$message.error("上传文件大小不能超过 1MB!");
          return false;
        }
        //如果文件类型不在允许上传的范围内
        if (this.fileType.includes(FileExt)) {
          return true;
        } else {
          this.$message.error("上传文件格式不正确!");
          return false;
        }
      }
    },
    //这是我自定义的移除事件
    handleClosedelete(i) {
      this.fileList.splice(i, 1); //删除上传的文件
    },
    fileChange(file, fileList) {
      this.fileList = fileList;
    },
    // 压缩文件
    compressFile() {
      let config = {
        quality: 0.5, //默认0.5  0-1
        // width:100,
        // height:100
      };
      this.fileList.forEach(async (item) => {
        // console.log("this.fileList1", this.fileList1);
        console.log("原图size1===>", item.size);
        let result = await compressImage({
          file: item.raw,
          config,
          fileName: item.name,
        });
        console.log("压缩后图片size1===>", result.finalFile.size);
        // this.imageList.push(result);
        this.srcList2 = result.finalFile;
        // console.log("aaa", result.finalFile);
        // console.log("bbb", this.srcList2);
        // console.log("this.srcList", this.srcList);
      });
      // console.log("压缩后图片1===>", this.imageList);
    },
    //上传文件的事件
    uploadFiles(item) {
      this.$message("文件上传中........");
      //上传文件的需要formdata类型;所以要转
      this.compressFile();
      let timer = setTimeout(() => {
        uploadFiles(this.srcList2).then((res) => {
          if (res.data.status != false || res.data.id != null) {
            res.data.data.saveurl; //半截
            res.data.data.weburl; //全拼
            this.formLabelAlign.imageUrl1 = res.data.data.saveurl;
            this.formLabelAlign.water_img1 = res.data.data.saveurl;
            this.formLabelAlign.imageUrl = res.data.data.weburl;
            this.fileList.push(this.srcList2); //成功过后手动将文件添加到展示列表里
            let i = this.fileList.indexOf(this.srcList2);
            this.handleSuccess();
          }
        });
        clearTimeout(timer);
      }, 1000);
      // uploadFiles(item.file).then((res) => {
      //   if (res.data.status != false || res.data.id != null) {
      //     res.data.data.saveurl; //半截
      //     res.data.data.weburl; //全拼
      //     this.formLabelAlign.imageUrl1 = res.data.data.saveurl;
      //     this.formLabelAlign.water_img1 = res.data.data.saveurl;
      //     this.formLabelAlign.imageUrl = res.data.data.weburl;
      //     this.fileList.push(item.file); //成功过后手动将文件添加到展示列表里
      //     let i = this.fileList.indexOf(item.file);
      //     this.handleSuccess();
      //   }
      // });
    },
    //上传成功后的回调
    handleSuccess() {
      this.$message({
        message: "上传成功",
        type: "success",
      });
      this.handleClosedelete();
    },
    //上下架修改
    swiCage(id, is_up) {
      ticketUser(this.token, id, is_up)
        .then((res) => {
          if (res.data.status) {
            this.invoking();
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    switchChange(row) {
      console.log(row);
    },
    switchCage(index, row) {
      this.swiCage(row.id, row.is_up);
    },
    //获取列表数据
    invoking() {
      this.loading = true;
      ticketing(
        this.token,
        this.page,
        this.limit,
        this.formInline.name,
        this.formInline.waterCatId,
        this.formInline.isUp
      )
        .then((res) => {
          if (res.data.status) {
            this.tableData = res.data.data.data;
            // is_hot
            this.total = res.data.data.count;
            this.tableData.forEach((item) => {
              if (item.is_hot == null) {
                item.is_hot = 2;
              }
              if (item.water_img) {
                item.water_img1 = item.water_img;
                item.water_img = this.imgHeader + item.water_img;
              }
            });

            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //编辑
    handle(row) {
      if (row.is_up == 1) {
        this.$message.warning("上架时不能编辑！");
        return;
      }
      this.dialogTitle = "编辑";
      let obj = JSON.parse(JSON.stringify(row));
      this.formLabelAlign = cloneDeep(row);
      console.log(this.formLabelAlign);
      this.formLabelAlign.imageUrl = obj.water_img;
      this.selectGoodsDate.name = cloneDeep(row.goods_name);
      this.selectGoodsDate.name = obj.goods_name;
      this.selectGoodsDate.id = cloneDeep(row.goods_id);
      this.dialogVisible = true;
    },

    //商品分类
    sdhadj() {
      this.loading = true;
      classification(this.token)
        .then((res) => {
          if (res.data.status) {
            let list = res.data.data.data;
            // this.Data = list.filter((e) => {
            //   return e.name != "套餐热销" && e.name != "热销";
            // });
            this.Data = list.filter((e) => {
              return e.status != 2;
            });
            // console.log("this.Data", this.Data);
            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //添加商品
    handleSelectionChange(val) {
      if (val) {
        this.selectGoodsDate = cloneDeep(val);

        this.multipleSelection = val;
        this.exhibition = val.name;
      }
    },
    onSubmitUser() {
      this.getInnerGoodsList();
    },
    // 删除
    handleEdit(val) {
      if (val.is_up == 1) {
        this.$message.warning("上架时不能删除！");
        return;
      }
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          Package_del(this.token, val.id).then((res) => {
            if (res.data.status) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.invoking();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.sdhadj();
    this.invoking();
  },
};
</script>

<style scoped>
#app {
  width: 100%;
}

.selectTicket {
  border: 1px solid rgb(202, 196, 196);
  height: 25px;
  width: 48px;
  font-size: 17px;
  padding: 5px 10px;
  margin: 0 10px;
}

.box {
  margin-left: 20px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.oneLine {
  display: flex;
  justify-content: space-between;
  margin-bottom: 14px;
}

.bucketUnit {
  margin-right: 49%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.distance {
  margin-right: 25%;
}

.edge {
  margin: 15px 0;
}

.demonstrate {
  margin-bottom: 20px;
}

.innerTab .is-scrolling-none .current-row .el-table__cell {
  background-color: rgb(108, 71, 241) !important;
}

.buttonUser {
  margin-left: 90%;
}
</style>
